"use strict";

var timeout;
app.controller("QueryFeeDetailsController", ["$scope", "$modal", "$timeout", 'Excel', "RESTService",
    function ($scope, $modal, $timeout, Excel, RESTService) {

        $scope.servReqs = [];
        $scope.item = {
            startDate: moment(new Date()).format('YYYY-MM-01'),
            finishDate: moment(new Date()).format('YYYY-MM-DD')
        }
        $scope.summaryOfPaid_Amount = 0;
        $scope.summaryOfAmount_Operation = 0;
        $scope.summaryOfAmount_Regional = 0;
        $scope.summaryOfAmount_Expert = 0;
        $scope.summaryOfAmount_Operator = 0;
        $scope.summaryOfAmount_Platform = 0;

        var loadListData = function (needExcel) {
            var startDate = moment($scope.item.startDate).format('YYYY-MM-DD');
            var finishDate = moment($scope.item.finishDate).format('YYYY-MM-DD');
            RESTService.getResponse("/rest/servreq/getFeeDetails?like=&startDate=" + startDate + "&finishDate=" + finishDate).then(function (response) {
                $scope.servReqs = response;

                $scope.summaryOfPaid_Amount = 0;
                $scope.summaryOfAmount_Operation = 0;
                $scope.summaryOfAmount_Regional = 0;
                $scope.summaryOfAmount_Expert = 0;
                $scope.summaryOfAmount_Operator = 0;
                $scope.summaryOfAmount_Platform = 0;

                angular.forEach(response, function (item) {
                    $scope.summaryOfPaid_Amount += item.paid_Amount;
                    $scope.summaryOfAmount_Operation += item.amount_Operation;
                    $scope.summaryOfAmount_Regional += item.amount_Regional;
                    $scope.summaryOfAmount_Expert += item.amount_Expert;
                    $scope.summaryOfAmount_Operator += item.amount_Operator;
                    $scope.summaryOfAmount_Platform += item.amount_Platform;
                });

                if(needExcel){
                    $timeout(function(){
                        $scope.exportHref=Excel.tableToExcel(needExcel,'sheet name');
                        location.href=$scope.exportHref;
                    },100);
                }
            });
        }
        $scope.init = function () {
            loadListData();
        }
        $scope.query = function () {
            loadListData();
        }

        $scope.exportToExcel=function(exportToExcel){
            loadListData(exportToExcel);
        }
    }]);
